<?php

namespace app\serversetting\traits;
use \app\manage\service\ApiBase;

/**
 * @author:      李颖
 * @DateTime:    2017-08-17 16:10:40
 * @Description: 添加一条记录<该记录拥有分类>时，检验分类的存在性和app_id
 * $data : 两个参数： app_id 、 cate_id或category_id
 */

trait AddCheckAppId
{

	private function _addCheckAppId($data, $logic, $dbCateInfoMethod = 'dbCateInfo', $cateIdFieldName = 'category_id'): array 
	{
		$cateInfo = $logic->$dbCateInfoMethod(['id'=>$data[$cateIdFieldName]]);
        if(!$cateInfo || $cateInfo['status'] != 1){
        	return auto_failure(null, '该分类不存在或被禁用');
        }
        //添加时，检验app_id的合法性
        $manageAppId = ApiBase::getAppId();
        $cateInfo['other_app_read'] = explode(",", $cateInfo['other_app_read']);
        if($manageAppId != $data['app_id'] && ($cateInfo['app_id'] != $data['app_id'] && !in_array($data['app_id'],$cateInfo['other_app_read']) && $cateInfo['is_public'] != 1)){
            return verify_err(null, '所选分类不属于该应用');
        }
        return auto_success($data, '分类检验成功');
	}





}